package com.example.a2306_shop

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.example.a2306_shop.adapter.ProductAdapter
import com.example.a2306_shop.adapter.ProductMuAdapter
import com.example.a2306_shop.databinding.ActivityMainBinding
import com.example.a2306_shop.main.MainViewModel
import com.example.lib_base.mvvm.BaseActivity
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 * 模块化组件化
 * 组件化：lib_base
 * 模块：user  在gradle文件中添加了lib_base
 * 主项目:app  在gradle文件中添加了 lib_base   user
 */
@Route(path = "/app/MainActivity")
class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    //lateinit var productMuAdapter: ProductMuAdapter
    lateinit var bottom:BottomNavigationView
    @Autowired
    @JvmField
    var index = 0
    override fun initView() {
        ARouter.getInstance().inject(this)
        //只能底部点击  不能左右滑动
        //找到fragment
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        //找到导航器
        val navController = navHostFragment.navController
        //和底部bottom进行联动
        binding.bottom.setupWithNavController(navController)

        binding.bottom.getOrCreateBadge(R.id.cartFragment).number = 3

        //将bottomNavigationView设成全局变量，方便其他地方调用
        bottom = binding.bottom

        when(index){
            0->binding.bottom.selectedItemId = R.id.homeFragment
            1->binding.bottom.selectedItemId = R.id.categoryFragment
            2->binding.bottom.selectedItemId = R.id.cartFragment
            3->binding.bottom.selectedItemId = R.id.mineFragment
        }


        /* productMuAdapter = ProductMuAdapter()
         binding.rv.adapter = productMuAdapter
         binding.rv.layoutManager = LinearLayoutManager(this)

         viewModel.product("",10,1)
         //搜索功能
         binding.et.addTextChangedListener(object :TextWatcher{
             override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

             }

             override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s!=null){
                    viewModel.product(s.toString(),10,1)
                }
             }

             override fun afterTextChanged(s: Editable?) {

             }
         })*/

        //动态获取权限
        requestPermissions(arrayOf("android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"),100)

    }

    override fun initData() {

       /* viewModel.productSuccess.observe(this){
            if(it.code==200){
                it.data.list.forEachIndexed { index, product ->
                    product.type = index%2
                }
                productMuAdapter.data.clear()
                productMuAdapter.data.addAll(it.data.list)
                productMuAdapter.notifyDataSetChanged()
            }
        }*/
    }



    override fun getViewModel(): Class<MainViewModel> = MainViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_main

}